package file;

import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapTest {

	public static void listHashMap() {
		HashMap hashmap = new HashMap();
		for (int i = 0; i < 1000; i++) {
			hashmap.put(i, "thanks");
		}
		long bs = Calendar.getInstance().getTimeInMillis();
		Iterator it = hashmap.entrySet().iterator();
		while (it.hasNext()) {
			java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
			// entry.getKey() 返回与此项对应的键
			// entry.getValue() 返回与此项对应的值
			System.out.print(entry.getValue());
		}
		System.out.println();
		System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
	}
	
	public static void test1(){
		HashMap hashmap = new HashMap();
		for (int i = 0; i < 1000; i++) {
			hashmap.put(i, "thanks");
		}

		long bs = Calendar.getInstance().getTimeInMillis();
		Iterator iterator = hashmap.keySet().iterator();
		while (iterator.hasNext()) {
			System.out.print(hashmap.get(iterator.next()));
		}
		System.out.println();
		System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
	}
	
	public static void test2(){
		   HashMap myMap = new HashMap(); 
		   myMap.put("hello", "你好"); 
		   myMap.put("bye", "再见"); 
		   myMap.put("thanks", "谢谢"); 
		   myMap.put("ok", "好的"); 
		   System.out.println("--------------------遍历key和value----------------------"); 
		   for(Iterator iter = myMap.entrySet().iterator();iter.hasNext();){ 
		             Map.Entry element = (Map.Entry)iter.next(); 
		             Object strKey = element.getKey(); 
		             Object strObj = element.getValue(); 
		             System.out.println("myMap.get(\""+strKey+"\")="+strObj); 
		   } 
		   System.out.println(); 
		   System.out.println("--------------------遍历整个HashMap----------------------"); 
		   Collection objs = myMap.entrySet(); 
		   for (Iterator iterator=objs.iterator(); iterator.hasNext();){ 
		    Object obj = iterator.next(); 
		    System.out.println(obj); 
		   } 
		   System.out.println(); 
		   System.out.println("--------------------遍历HashMap的key----------------------"); 
		   Collection keys = myMap.keySet(); 
		   for (Iterator iterator=keys.iterator(); iterator.hasNext();){ 
		    Object key = iterator.next(); 
		    System.out.println(key); 
		   } 
		   System.out.println(); 
		   System.out.println("--------------------遍历HashMap的value----------------------"); 
		   Collection values = myMap.values(); 
		   for (Iterator iterator=values.iterator(); iterator.hasNext();){ 
		    Object value = iterator.next(); 
		    System.out.println(value); 
		   }
	   }
	
	public static void main(String[] args) {
		test1();
		listHashMap();
		test2();
	}
}